package com.fun.demo.fun;

import java.io.*;

public class TextSplit {
    public static void main(String[] args) throws IOException {
        String txtPath = "D:/迅雷下载/xmanager5/";
        String filename = "lifeihai";
        File file = new File(txtPath + filename + ".txt");
        BufferedReader fr = new BufferedReader(new FileReader(file));

        int totalsize = filesize(file);
        System.out.println("totale size:" + totalsize + "MB");

        int setpartsize = 3;
        String newfileName = "";
        BufferedWriter fw = null;

        String line = "";
        for (int i = 1; line != null; i++) {
            newfileName = txtPath + filename + "-" + i + ".txt";
            file = new File(newfileName);
            fw = new BufferedWriter(new FileWriter(file));
            while (filesize(file) < setpartsize && (line = fr.readLine()) != null) {
                fw.write(line);
                fw.newLine();
            }
            System.out.println(i);
        }
        fr.close();
        fw.close();
    }

    private static int filesize(File file) throws IOException //返回文件大小
    {
        FileInputStream fis = new FileInputStream(file);
        int size = fis.available() / (1024 * 1024);
        fis.close();
        return size;
    }


}
